package com.far;

import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class TextToSpeechActivity extends Activity implements OnInitListener, OnClickListener {

    //TTS object
    private TextToSpeech myTTS;
    //TTS Check Code
    private int TTS_CHECK_CODE = 1337;
    
    public void onCreate(Bundle savedInstanceState) {
         
        super.onCreate(savedInstanceState);
        setContentView(R.layout.speech);
        Button speakButton = (Button)findViewById(R.id.speak);
        speakButton.setOnClickListener(this);

        Intent checkTTSIntent = new Intent();
        checkTTSIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(checkTTSIntent, TTS_CHECK_CODE);
}
        
        @Override
        public void onClick(View v) {
        EditText enteredText = (EditText)findViewById(R.id.enter);
        String words = enteredText.getText().toString();
        if(words.length() > 0) speakWords(words);
        }
        
        private void speakWords(String speech) {         
        //speak straight away
        myTTS.speak(speech, TextToSpeech.QUEUE_FLUSH, null);
        }
        
        @Override
    	public void onAttachedToWindow(){
    		super.onAttachedToWindow();
    		Window window = getWindow();
    		window.setFormat(PixelFormat.RGBA_8888);
    	}
        
        //act on result of TTS data check
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 
        if (requestCode == TTS_CHECK_CODE) {
            if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
            //the user has the necessary data - create the TTS
            myTTS = new TextToSpeech(this, this);
            }
            else {
                    //no data - install it now
                Intent installTTSIntent = new Intent();
                installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                startActivity(installTTSIntent);
            }
        }
    }

        @Override
        public void onInit(int initStatus) {     
                //check for successful instantiation
            if (initStatus == TextToSpeech.SUCCESS) {
                if(myTTS.isLanguageAvailable(Locale.US)==TextToSpeech.LANG_AVAILABLE)
                    myTTS.setLanguage(Locale.US);
            }
            else if (initStatus == TextToSpeech.ERROR) {
                Toast.makeText(this, "Sorry! Text To Speech failed to start...", Toast.LENGTH_LONG).show();
            }
        }
        
        @Override
        public void onDestroy(){
        	if(myTTS != null){
        		myTTS.stop();
        		myTTS.shutdown();
        	}
        	super.onDestroy();
        }

}
